<template>
  <div class="home-view">
    <!-- 轮播横幅 - 全屏宽度 -->
    <section class="hero-section hero-section-fullwidth">
      <el-carousel height="400px" :interval="4000" arrow="hover">
        <el-carousel-item v-for="(banner, index) in banners" :key="index">
          <div class="banner-item" :style="{ backgroundImage: `url(${banner.image})` }">
            <div class="banner-content">
              <h2>{{ banner.title }}</h2>
              <p>{{ banner.description }}</p>
              <el-button type="primary" size="large" @click="$router.push('/publish')">
                立即发布
              </el-button>
            </div>
          </div>
        </el-carousel-item>
      </el-carousel>
    </section>

    <!-- 快速分类 - 全屏宽度 -->
    <section class="categories-section categories-section-fullwidth">
      <div class="container">
        <div class="section-header">
          <h2>商品分类</h2>
          <p>快速找到你需要的商品</p>
        </div>
        <div class="categories-grid">
          <div 
            v-for="category in goodsStore.categories" 
            :key="category.categoryId"
            class="category-item"
            @click="filterByCategory(category.name)"
          >
            <div class="category-icon">{{ category.icon }}</div>
            <span class="category-name">{{ category.name }}</span>
          </div>
        </div>
      </div>
    </section>

    <!-- 筛选和排序 - 全屏宽度 -->
    <section class="filter-section filter-section-fullwidth">
      <div class="container">
        <div class="filter-bar">
          <div class="filter-left">
            <el-button-group>
              <el-button 
                :type="sortBy === 'latest' ? 'primary' : ''"
                @click="handleSort('latest')"
              >
                最新发布
              </el-button>
              <el-button 
                :type="sortBy === 'price-low' ? 'primary' : ''"
                @click="handleSort('price-low')"
              >
                价格从低到高
              </el-button>
              <el-button 
                :type="sortBy === 'price-high' ? 'primary' : ''"
                @click="handleSort('price-high')"
              >
                价格从高到低
              </el-button>
              <el-button 
                :type="sortBy === 'popular' ? 'primary' : ''"
                @click="handleSort('popular')"
              >
                最受欢迎
              </el-button>
            </el-button-group>
          </div>
          
          <div class="filter-right">
            <el-button 
              :icon="Refresh" 
              @click="refreshGoods"
              :loading="goodsStore.isLoading"
            >
              刷新
            </el-button>
          </div>
        </div>
      </div>
    </section>

    <!-- 商品列表 - 全屏宽度 -->
    <section class="goods-section goods-section-fullwidth">
      <div class="container">
        <!-- 加载状态 -->
        <div v-if="goodsStore.isLoading" class="loading-container">
          <el-skeleton :rows="6" animated />
        </div>

        <!-- 商品网格 -->
        <div v-else-if="goodsStore.goodsList.length > 0" class="goods-grid">
          <GoodsCard 
            v-for="goods in goodsStore.goodsList" 
            :key="goods.goodsId"
            :goods="goods"
          />
        </div>

        <!-- 空状态 -->
        <div v-else class="empty-state">
          <el-empty description="暂无商品">
            <template #image>
              <div class="empty-icon">📦</div>
            </template>
            <el-button type="primary" @click="$router.push('/publish')">
              发布第一个商品
            </el-button>
          </el-empty>
        </div>
      </div>
    </section>

    <!-- 分页 -->
    <Pagination
      :total="goodsStore.pagination.total"
      :page="goodsStore.pagination.page"
      :limit="goodsStore.pagination.limit"
      @page-change="handlePageChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useGoodsStore } from '@/stores/goodsStore'
import { useRouter } from 'vue-router'
import GoodsCard from '@/components/GoodsCard.vue'
import Pagination from '@/components/Pagination.vue'
import { Refresh } from '@element-plus/icons-vue'

const goodsStore = useGoodsStore()
const router = useRouter()

// 排序方式
const sortBy = ref('latest')

// 轮播横幅数据
const banners = ref([
  {
    title: '欢迎来到江农二手市场',
    description: '让闲置物品重新焕发价值，为环保贡献一份力量',
    image: 'https://images.pexels.com/photos/4386321/pexels-photo-4386321.jpeg?auto=compress&cs=tinysrgb&w=1200'
  },
  {
    title: '安全可靠的交易环境',
    description: '校园内部交易，师生身份认证，交易更安心',
    image: 'https://images.pexels.com/photos/5632402/pexels-photo-5632402.jpeg?auto=compress&cs=tinysrgb&w=1200'
  },
  {
    title: '发布你的闲置物品',
    description: '简单几步，让你的闲置物品找到新主人',
    image: 'https://images.pexels.com/photos/4386431/pexels-photo-4386431.jpeg?auto=compress&cs=tinysrgb&w=1200'
  }
])

// 按分类筛选
const filterByCategory = (category) => {
  goodsStore.filterGoods({ category })
}

// 处理排序
const handleSort = (type) => {
  sortBy.value = type
  let sortParams = {}
  
  switch (type) {
    case 'latest':
      sortParams = { sortBy: 'createdAt', sortOrder: 'desc' }
      break
    case 'price-low':
      sortParams = { sortBy: 'price', sortOrder: 'asc' }
      break
    case 'price-high':
      sortParams = { sortBy: 'price', sortOrder: 'desc' }
      break
    case 'popular':
      sortParams = { sortBy: 'viewCount', sortOrder: 'desc' }
      break
  }
  
  goodsStore.getGoodsList(sortParams)
}

// 刷新商品列表
const refreshGoods = () => {
  goodsStore.resetSearch()
  goodsStore.getGoodsList()
  sortBy.value = 'latest'
}

// 处理分页
const handlePageChange = (page) => {
  goodsStore.changePage(page)
}

const handleSizeChange = (size) => {
  goodsStore.pagination.limit = size
  goodsStore.pagination.page = 1
  goodsStore.getGoodsList()
}

// 组件挂载时获取商品列表
onMounted(() => {
  goodsStore.getGoodsList()
})
</script>

<style scoped>
.home-view {
  min-height: calc(100vh - 200px);
}

/* 轮播横幅样式 */
.hero-section {
  margin-bottom: 40px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

/* 全屏宽度轮播横幅 */
.hero-section-fullwidth {
  margin: 0 0 40px 0;
  border-radius: 0;
  box-shadow: none;
  width: 100vw;
  position: relative;
  left: 50%;
  right: 50%;
  margin-left: -50vw;
  margin-right: -50vw;
}

.hero-section-fullwidth .banner-item {
  height: 400px;
}

.banner-item {
  height: 300px;
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.banner-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(0, 0, 0, 0.4) 0%, rgba(0, 0, 0, 0.2) 100%);
}

.banner-content {
  text-align: center;
  color: white;
  z-index: 1;
  max-width: 600px;
  padding: 0 20px;
}

.banner-content h2 {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 15px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.banner-content p {
  font-size: 18px;
  margin-bottom: 25px;
  opacity: 0.9;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

/* 分类区域样式 */
.categories-section {
  margin-bottom: 40px;
}

/* 全屏宽度分类区域 */
.categories-section-fullwidth {
  background: #f8f9fa;
  margin: 0 -20px 40px -20px;
  padding: 40px 20px;
}

.categories-section-fullwidth .container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
}

.section-header {
  text-align: center;
  margin-bottom: 30px;
}

.section-header h2 {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.section-header p {
  font-size: 16px;
  color: #666;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 25px 15px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s ease;
}

.category-item:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.category-icon {
  font-size: 36px;
  margin-bottom: 12px;
}

.category-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

/* 筛选区域样式 */
.filter-section {
  margin-bottom: 30px;
}

/* 全屏宽度筛选区域 */
.filter-section-fullwidth {
  background: #f8f9fa;
  margin: 0 -20px 30px -20px;
  padding: 20px;
}

.filter-section-fullwidth .container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
}

.filter-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  padding: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

/* 商品区域样式 */
.goods-section {
  margin-bottom: 40px;
}

/* 全屏宽度商品区域 */
.goods-section-fullwidth {
  background: #f8f9fa;
  margin: 0 -20px 40px -20px;
  padding: 40px 20px;
}

.goods-section-fullwidth .container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
}

.loading-container {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.goods-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.empty-state {
  background: white;
  padding: 60px 20px;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  text-align: center;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 20px;
  opacity: 0.5;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .categories-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 18px;
  }
  
  /* 全屏宽度布局响应式 */
  .hero-section-fullwidth {
    margin: 0 -15px 40px -15px;
  }
  
  .categories-section-fullwidth,
  .filter-section-fullwidth,
  .goods-section-fullwidth {
    margin: 0 -15px 40px -15px;
    padding: 30px 15px;
  }
}

@media (max-width: 768px) {
  .banner-content h2 {
    font-size: 24px;
  }
  
  .banner-content p {
    font-size: 16px;
  }
  
  .categories-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
  }
  
  .category-item {
    padding: 20px 10px;
  }
  
  .category-icon {
    font-size: 28px;
  }
  
  .filter-bar {
    flex-direction: column;
    gap: 15px;
    align-items: stretch;
  }
  
  .filter-left {
    overflow-x: auto;
  }
  
  .goods-grid {
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 15px;
  }
  
  /* 移动端全屏宽度布局 */
  .hero-section-fullwidth {
    margin: 0 -10px 30px -10px;
  }
  
  .hero-section-fullwidth .banner-item {
    height: 300px;
  }
  
  .categories-section-fullwidth,
  .filter-section-fullwidth,
  .goods-section-fullwidth {
    margin: 0 -10px 30px -10px;
    padding: 20px 10px;
  }
}

@media (max-width: 480px) {
  .categories-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
  
  .category-item {
    padding: 15px 8px;
  }
  
  .category-icon {
    font-size: 24px;
  }
  
  .category-name {
    font-size: 12px;
  }
  
  .goods-grid {
    grid-template-columns: 1fr;
  }
}
</style>